package com.mojo.servlet;

import com.sun.deploy.net.HttpResponse;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 实现servlet
 * <p>
 * 请求和相应在底层已经封装好,只需要实现doGet()或doPost()方法类做具体逻辑操作
 * </p>
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 */
@WebServlet(urlPatterns = "/")
public class HelloServlet02 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        HttpSession session = req.getSession();
        req.getAttribute("");
        session.getAttribute("");
        ServletContext servletContext = session.getServletContext();
        Object name = req.getAttribute("name");
        // 设置响应类型:
        resp.setContentType("text/html");
        // 获取输出流:
        PrintWriter pw = resp.getWriter();
        // 写入响应:
        pw.write("<h1>Hello, world!" + name + "</h1>");
        // 最后不要忘记flush强制输出:
        pw.flush();
        //重定向
        //resp.sendRedirect("localhost:8080");
        //设置状态码及信息
        //resp.sendError(400,"页面错误");
    }
}